package com.doudou.base.resources.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doudou.base.resources.dao.OauthClientDetailsDao;
import com.doudou.base.resources.entity.AppClientDetails;
import com.doudou.base.resources.entity.dto.oauthclientdetails.AppClientDetailsAddDto;
import com.doudou.base.resources.entity.dto.oauthclientdetails.AppClientDetailsUpdateDto;
import com.doudou.base.resources.service.AppClientDetailsService;
import com.doudou.client.entity.AppClientDetailsDto;
import com.doudou.commons.utils.ConvertBeanUtils;
import com.doudou.commons.utils.ListUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**>
 *  服务实现类
 *
 * @author 傻男人
 * @since 2020-05-08
 */
@Slf4j
@Service
public class AppClientDetailsServiceImpl implements AppClientDetailsService {

    @Autowired
    OauthClientDetailsDao oauthClientDetailsDao;

    /**
    * 添加
    * @param appClientDetailsAddDto
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppClientDetailsDto addOauthClientDetails(AppClientDetailsAddDto appClientDetailsAddDto) {
        AppClientDetails appClientDetails = ConvertBeanUtils.copyProperties(appClientDetailsAddDto, AppClientDetails.class);
        oauthClientDetailsDao.insert(appClientDetails);
        AppClientDetailsDto appClientDetailsDto = ConvertBeanUtils.copyProperties(appClientDetails, AppClientDetailsDto.class);
        return appClientDetailsDto;
    }

    /**
     * 根据ids删除
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteOauthClientDetails(String ids) {
        List<Long> listId = ListUtils.strToLongList(ids);
        UpdateWrapper<AppClientDetails> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in(AppClientDetails.Meta.id,listId);

        AppClientDetails appClientDetails = new AppClientDetails();
        appClientDetails.setDelFlag(1);
        appClientDetails.setUpdateDate(new Date());
        Integer update = oauthClientDetailsDao.update(appClientDetails, updateWrapper);
        return update;
    }

    /**
     * 根据id修改
     * @param appClientDetailsUpdateDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateOauthClientDetails(AppClientDetailsUpdateDto appClientDetailsUpdateDto) {
        //检查传入的id是否正确
        AppClientDetails appClientDetails = oauthClientDetailsDao.selectById(appClientDetailsUpdateDto.getId());
        if(null == appClientDetails){
            log.info("修改的数据不存在");
            return null;
        }
        appClientDetails = ConvertBeanUtils.copyProperties(appClientDetailsUpdateDto, AppClientDetails.class);
        return oauthClientDetailsDao.updateById(appClientDetails);
    }

    /**
     * 根据ids查询
     * @param ids
     * @return
     */
    @Override
    public List<AppClientDetailsDto> findOauthClientDetails(String ids) {
        List<Long> listIds = ListUtils.strToLongList(ids);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in(AppClientDetails.Meta.id,listIds);
        List<AppClientDetails> appClientDetailsses = oauthClientDetailsDao.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(appClientDetailsses)){
            return ListUtils.copyList(appClientDetailsses, AppClientDetailsDto.class);
        }
        return null;
    }

    /**
     * 根据id查询
     * @param oauthClientDetailsId
     * @return
     */
    @Override
    public AppClientDetailsDto findOauthClientDetailsById(Long oauthClientDetailsId){
        AppClientDetails appClientDetails = oauthClientDetailsDao.selectById(oauthClientDetailsId);
        return ConvertBeanUtils.copyProperties(appClientDetails, AppClientDetailsDto.class);
    }

    @Override
    public AppClientDetailsDto findClientByClientId(String clientId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(AppClientDetails.Meta.clientId,clientId);
        queryWrapper.eq(AppClientDetails.Meta.delFlag,0);
        List<AppClientDetails> result = oauthClientDetailsDao.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(result)){
            return ConvertBeanUtils.copyProperties(result.get(0),AppClientDetailsDto.class);
        }
        return null;
    }


}
